package com.lshl.lingshang.pub;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.lshl.lingshang.application.MyApplication;
import com.lshl.lingshang.ui.bean.UserBean;
import com.lshl.lingshang.utils.AppUtil;
import com.lshl.lingshang.utils.FileUtil;
import com.lshl.lingshang.utils.JSONUtil;
import com.lshl.lingshang.utils.SharedPUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

/**
 * 获取修改保存到本地数据的方法类
 */
public class DataModule {
    private static DataModule instance = null;
    public HashMap<String, Object> registerInfoMap = new HashMap<String, Object>();

    public static DataModule getInstance() {
        if (instance == null) {
            instance = new DataModule();
        }

        return instance;
    }

    /**
     * 获取已登录用户
     */
    public UserBean getLoginUserInfo() {
        try {
            UserBean pb = (UserBean) SharedPUtil.deSerialize(PubConst.USER + getLoginedUserId());
            return pb;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 直接保存
     */
    public void saveList(List list, String keyword) {
        if (list == null || list.size() == 0) return;
        JSONUtil.updateDict(list, keyword);
    }

    /**
     * 保存已登录用户信息
     */
    public void saveLoginedUserInfo(UserBean user) {
        saveLoginUserInfo(user);
    }

    /**
     * 保存已登录用户
     */
    private void saveLoginUserInfo(UserBean user) {
        try {
            if (user == null) return;
            //单独保存userid  方便使用
            SharedPUtil.setInt(PubConst.KEY_LOGINED_USERID, user.getUid());
            SharedPUtil.serialize(user, PubConst.USER + user.getUid());
            SharedPUtil.setString(PubConst.KEY_MOBILE, user.getTelephone());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    /**
     * 退出登录，清除登录记录
     */
    public void logout() {
        removeTencentQuickLoginInfo();
        removeLoginedUserInfo();
        removeNIMLoginedUserInfo();
    }

    private void removeNIMLoginedUserInfo() {
        SharedPUtil.removeData(PubConst.NIM_ACCOUNT);
        SharedPUtil.removeData(PubConst.NIM_USER);
    }

    public void removeLoginedUserInfo() {
        SharedPUtil.removeData(PubConst.USER + getLoginedUserId());
        SharedPUtil.setString(PubConst.KEY_MOBILE, "");
        SharedPUtil.setInt(PubConst.KEY_LOGINED_USERID, 0);
//        SharedPUtil.removeData(PubConst.KEY_LOGINED_USERID);
//        SharedPUtil.removeData(PubConst.KEY_MOBILE);
        SharedPUtil.removeData(PubConst.KEY_LOGINED_USERTYPE);
        SharedPUtil.removeData(PubConst.KEY_LOGINED_LINE_STATUS);
        SharedPUtil.removeData(PubConst.KEY_LOGINED_LINE_EASY_TEACHER_STATUS);
    }

    /**
     * 删除第三方登录的数据
     */
    public void removeTencentQuickLoginInfo() {
//        SharedPUtil.removeData(PubConst.THIRD_ID);
//        SharedPUtil.removeData(PubConst.THIRD_NAME);
//        SharedPUtil.removeData(PubConst.THIRD_SEX);
//        SharedPUtil.removeData(PubConst.THIRD_ICONURL);
    }

    public static float getBMI(int height, float weight) {
        if (height == 0 || weight == 0) return 0;
        float bmi = weight / (((float) height) / 100 * 2);
        return bmi;
    }


    /**
     * 字典文件存储路径
     *
     * @return
     */
    public static String getDictFilePath() {
        return MyApplication.getInstance().getFilesDir().toString();
    }

    /**
     * 字典： 调用方法：List<GlucometerBean> a =
     * DataModule.loadDict(MyProfile.DICT_GLUCOMETERS, GlucometerBean.class);
     */
    public static <T> List<T> loadDict(String dictName, Class<T> classOfT) {

        String filename = String.format(Locale.CHINESE, "%s/%s.%s", getDictFilePath(), dictName, PubConst.DICT_FILE_EXT);
        String json_string = FileUtil.readFile(filename);
        if (json_string == null) return null;

        try {
            Gson gson = new Gson();
            List<T> dataList = new ArrayList<T>();
            JSONArray ja = new JSONArray(json_string);
            for (int i = 0; i < ja.length(); i++) {
                JSONObject jo = ja.getJSONObject(i);

                T gb = gson.fromJson(jo.toString(), classOfT);
                if (gb != null) dataList.add(gb);
            }

            return dataList;

        } catch (Exception e) {
        }

        return null;
    }


    /**
     * 取得登录用户ID
     *
     * @return
     */
    public int getLoginedUserId() {
        return SharedPUtil.getInt(PubConst.KEY_LOGINED_USERID, 0);
    }

    /**
     * 获得登录的老师状态
     *
     * @return
     */
    public int getLoginedUserType() {
        return SharedPUtil.getInt(PubConst.KEY_LOGINED_USERTYPE, 0);
    }

    /**
     * 获得是否有连线状态权限
     *
     * @return
     */
    public int getLineStatus() {
        return SharedPUtil.getInt(PubConst.KEY_LOGINED_LINE_STATUS, 0);
    }

    /**
     * 获得老师在线状态
     *
     * @return
     */
    public int getTeacherOnlineStatus() {
        return SharedPUtil.getInt(PubConst.KEY_LOGINED_LINE_EASY_TEACHER_STATUS, 0);
    }

    /**
     * 设置老师在线状态
     *
     * @return
     */
    public void setTeacherOnlineStatus(int teacherOnlineStatus) {
        SharedPUtil.setInt(PubConst.KEY_LOGINED_LINE_EASY_TEACHER_STATUS, teacherOnlineStatus);
    }


    /**
     * 获取当前设备的唯一识别码 如无唯一识别码，则新生成一个
     *
     * @return
     */
    public String getUUID(Context context) {
        String uuid = SharedPUtil.getString(PubConst.KEY_UUID, "");
        if (TextUtils.isEmpty(uuid)) {
            uuid = AppUtil.getUUID(context);
            SharedPUtil.setString(PubConst.KEY_UUID, uuid);
        }
        return uuid;
    }

//    /**
//     * 获取渠道ID
//     *
//     * @return
//     */
//    public String getChannelId() {
//        String channelName = AnalyticsConfig.getChannel(MyApplication.getInstance().getApplicationContext());
//        return channelName;
//    }

    /**
     * 获取首次启动标记
     *
     * @param context
     * @return
     */
    public int getIsStarted(Context context) {
        return SharedPUtil.getInt(PubConst.KEY_ISSTARTED + AppUtil.getVersionCode(context), 1);
    }

    /**
     * 保存首次访问标记
     */
    public void saveIsStarted(Context context) {
        SharedPUtil.setInt(PubConst.KEY_ISSTARTED + AppUtil.getVersionCode(context), 2);
    }

    /**
     * 获取保存的手机号码
     *
     * @return
     */
    public String getMobile() {
        return SharedPUtil.getString(PubConst.KEY_MOBILE, "");
    }

    /**
     * 保存uuid
     *
     * @param uuid
     */
    public void saveUUid(String uuid) {
        SharedPUtil.setString(PubConst.KEY_UUID, uuid);
    }

    /**
     * 保存友盟token
     *
     * @return
     */
    public void saveUmengToken(String deviceToken) {
        SharedPUtil.setString(PubConst.UM_DEVICETOKEN, deviceToken);
    }

    /**
     * 获取友盟token
     *
     * @return
     */
    public String getUmengToken() {
        return SharedPUtil.getString(PubConst.UM_DEVICETOKEN, "");
    }

    /**
     * 保存 是否第一次进入app
     *
     * @param isFirst
     */
    public void saveIsFirst(boolean isFirst) {
        SharedPUtil.setBoolean(PubConst.ISFIRST, isFirst);
    }

    /**
     * 获取是否是是第一次进入app  默认 是
     *
     * @return
     */
    public boolean getIsFirst() {
        return SharedPUtil.getBoolean(PubConst.ISFIRST, true);
    }





    /**
     * 是否同意进入app的权限  默认否
     */
    public Boolean getAgreementLimits() {
       return SharedPUtil.getBoolean(PubConst.START_APP, false);
    }

    /**
     * 是否同意进入app的权限
     */
    public void setAgreementLimits(boolean start_app) {
        SharedPUtil.setBoolean(PubConst.START_APP, start_app);
    }

}
